//
//  ViewController.m
//  UI01_UIView
//
//  Created by aaaaaa on 16/6/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CustomView.h"

@interface ViewController ()

@property (nonatomic, retain) CustomView *rainView;

@end


@implementation ViewController


//对对象类型的属性进行内存管理 =_=    <*_*>
- (void)dealloc
{
    [_rainView release];
    [super dealloc];
}

#pragma mark - UIViewController生命周期

// 如果要重写UIViewController的初始化方法的话，一般都重写这个方法
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        NSLog(@"%d %s", __LINE__, __FUNCTION__);
    }
    return self;
    
}

-(instancetype)init{
    self = [super init];
    if (self) {
        NSLog(@"%d %s", __LINE__, __FUNCTION__);
    }
    return self;
}

//加载视图
-(void)loadView{
    [super loadView];
    NSLog(@"%d %s" , __LINE__, __FUNCTION__);
}

//视图加载完成, 加载子视图
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%d %s", __LINE__, __FUNCTION__);
    self.view.backgroundColor = [UIColor greenColor];
    
//    [self creadSubViews];
    [self addCoustomViews];
    [self creatButtons];
    
}

//视图将要显示
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"%d %s", __LINE__, __FUNCTION__);
}

//视图显示完成
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"%d %s", __LINE__, __FUNCTION__);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}









#pragma mark - 知识点2 UIView
// 创建自定义方法,加载子视图的代码
- (void)creadSubViews {
    
    //创建视图
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 200, 300)];
    
    //视图背景颜色默认为clearColor(透明)
    blueView.backgroundColor = [UIColor blueColor];
    //将子视图添加到父视图上, self.view是blueView的父视图
    [self.view addSubview:blueView];
    
    //内存管理
    [blueView release];
    
    //练习 创建1个视图 距离屏幕左边50，距离屏幕上边400，宽50， 高30， 背景颜色为黄色, 添加到self.view视图
    
    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(50, 400, 50, 30)];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
    [yellowView release];
    
    //将blackView放到blueView上
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 30, 50)];
    blackView.backgroundColor = [UIColor blackColor];
    [blueView addSubview:blackView];
    [blackView release];
    
    /*
     * frame 是以父视图为参照的坐标系统
     */
    
    // UIView常用API
    
    //隐藏视图，默认值为NO, 为YES时将视图隐藏
    //blueView.hidden = YES;
    
    // 改变视图的透明度,CGFloat类型，范围从0.0到1.0，默认为1.0， 1.0是完全不透明状态
    blueView.alpha = 0.5;
    
    // 获取视图的子视图
    NSLog(@"blueView SubViews : %@", blueView.subviews);
    
    // 获取视图的父视图(父视图只有唯一的一个)
    NSLog(@"blueView superView : %@", blueView.superview);
    
    // 视图中心点坐标(对角线交叉的地方)
    blueView.center = CGPointMake(100, 200);
    
    NSLog(@"x: %lf y : %lf w : %lf h : %lf", blueView.frame.origin.x, blueView.frame.origin.y, blueView.frame.size.width, blueView.frame.size.height);
    
    // 视图的标记 tag
    blueView.tag = 100;
    
    //通过tag找到指定视图
    UIView *findView = [self.view viewWithTag:100];
    
    NSLog(@"blueView %@", blueView);
    NSLog(@"findView %@", findView);
    
    // 输出一下self.view的子视图
    NSArray *svSubViewsArray = self.view.subviews;
    NSLog(@"self.view的子视图 %@", svSubViewsArray);

    // 创建一个视图，坐标为(50, 100)，宽高为(200, 300), 颜色为橙色，添加到self.view上
    UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 200, 300)];
    orangeView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:orangeView];
    [orangeView release];
    
    // 创建一个视图，坐标为(100, 150)，宽高为(200, 300), 颜色为紫色，添加到self.view上
    UIView *purpleView = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 200, 300)];
    purpleView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:purpleView];
    [purpleView release];
    
    
    //把blueView放到所有子视图的前面
    [self.view bringSubviewToFront:blueView];
    
    //把purpleView放到所有子视图的后面
    [self.view sendSubviewToBack:purpleView];
    
    // 创建视图深灰色视图，frame(),添加到下标为2的位置上
    UIView * darkGrayView = [[UIView alloc] initWithFrame:CGRectMake(120, 170, 200, 300)];
    darkGrayView.backgroundColor = [UIColor darkGrayColor];
    [self.view insertSubview:darkGrayView atIndex:2];
    [darkGrayView release];
    
    // 将视图writeView插入到darkGrayView前面
    UIView *writeView = [[UIView alloc] initWithFrame:CGRectMake(210, 180, 50, 50)];
    writeView.backgroundColor = [UIColor whiteColor];
    [self.view insertSubview:writeView aboveSubview:darkGrayView];
    [writeView release];
    
    // 将视图redView插入到darkGrayView后面
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(110, 160, 300, 400)];
    redView.backgroundColor = [UIColor redColor];
    [self.view insertSubview:redView belowSubview:darkGrayView];
    [redView release];
    
    // 交换指定下标位置上的视图
    [self.view exchangeSubviewAtIndex:4 withSubviewAtIndex:5];
    
    //将子视图从父视图上移除
    [redView removeFromSuperview];
}

// 添加自定义的视图
- (void)addCoustomViews {
    CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    customView.tag = 10;
    [self.view addSubview:customView];
    [customView release];
    
    CustomView *customColorView = [[CustomView alloc] initWithFrame:CGRectMake(200, 220, 100, 100) withColor:[UIColor yellowColor]];
    [self.view addSubview:customColorView];
    [customColorView release];
    
    //创建属性
    self.rainView = [[CustomView alloc] initWithFrame:CGRectMake(200, 300, 100, 100) withColor:[UIColor blackColor]];
    [self.view addSubview:self.rainView];
    [_rainView release];
}

#pragma mark - UIButton
- (void)creatButtons {
    
    //创建button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //设置背景色
    button.backgroundColor = [UIColor purpleColor];
    //确定在父视图的位置
    button.frame = CGRectMake(220, 120, 80, 30);
    //添加到父视图上
    [self.view addSubview:button];
    //不需要对button进行release
    
    //添加点击事件
    //UIControlEventTouchUpInside 按下的时候
    [button addTarget:self action:@selector(changeColorAction:) forControlEvents:UIControlEventTouchUpInside];

    
}

// parameterButton 就是 button
- (void)changeColorAction:(UIButton *)parameterButton{
    // 改变self.view的背景颜色
    self.view.backgroundColor = [UIColor blueColor];
    
    //通过tag值找到指定的视图
    CustomView *findView = (CustomView *)[self.view viewWithTag:10];
    findView.backgroundColor = [UIColor orangeColor];
    
    self.rainView.backgroundColor = [UIColor whiteColor];
    
}














@end
